package com.xindaoapp.happypet.activity;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.XinDaoBaseAdapter;
import com.xindaoapp.happypet.bean.MyRequestFriend;
import com.xindaoapp.happypet.bean.MyRequestFriend.FriendInfo;
import com.xindaoapp.happypet.ui.BaseActivity;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.view.CircleImageView;

/**
 * 个人中心--查看邀请的好友列表
 * 
 * @author Yangqing
 * 
 */
public class RequestFriendActivity extends BaseActivity {

    private PullToRefreshListView pullListView;
    private Context mContext;
    private List<FriendInfo> myFriends = new ArrayList<MyRequestFriend.FriendInfo>();
    private FriendAdapter friendAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        mContext = RequestFriendActivity.this;
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getContentViewResId() {
        return R.layout.layout_requestfriend;
    }

    @Override
    protected int getTopBarTitleRes() {
        return R.string.requestfriend;
    }

    @Override
    protected void initViews() {
        super.initViews();


        pullListView = (PullToRefreshListView) findViewById(R.id.scrollView);

        pullListView.setMode(Mode.PULL_FROM_START);
        pullListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                onLoadDatas();
            }
        });
        pullListView.getRefreshableView().setAdapter(null);
    }

    @Override
    protected void onLoadDatas() {
        super.onLoadDatas();
        getMoccaApi().getRequestFriends(new IRequest<MyRequestFriend>() {

            @Override
            public void request(MyRequestFriend t) {
                pullListView.onRefreshComplete();
                if (t != null) {
                    pullListView.onRefreshComplete();
                    onDataArrived(true);
                    if ("0".equals(t.getResult())) {
                        if (t.array.size() > 0) {
                            myFriends = t.array;
                            myFriends = t.array;
                            friendAdapter =
                                    new FriendAdapter(RequestFriendActivity.this, t.array, 100,
                                            R.layout.item_myrequestfriend, R.layout.layout_loading);
                            pullListView.getRefreshableView().setAdapter(friendAdapter);
                        } else {
                            onDataArrivedEmpty();
                        }
                    }
                } else {
                    onDataArrived(false);
                }
            }
        });

    }

    @Override
    protected void initEvents() {
        super.initEvents();

    }

    class FriendAdapter extends XinDaoBaseAdapter<FriendInfo> {

        public FriendAdapter(Context context, List<FriendInfo> iniData, int pageSize, int res,
                int loadingRes) {
            super(context, iniData, pageSize, res, loadingRes);
        }

        @Override
        public int getCount() {
            return myFriends.size();
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent, FriendInfo value) {
            ViewHolder holder = (ViewHolder) convertView.getTag();
            if (holder == null) {
                holder = new ViewHolder();
                // convertView = View.inflate(mContext, R.layout.item_myrequestfriend, null);
                holder.icon = (CircleImageView) convertView.findViewById(R.id.iv_icon);
                holder.friendName = (TextView) convertView.findViewById(R.id.tv_friendName);
                holder.friendCount = (TextView) convertView.findViewById(R.id.tv_friendCount);
                holder.date = (TextView) convertView.findViewById(R.id.tv_date);
                convertView.setTag(holder);
            }

            FriendInfo friendInfo = myFriends.get(position);

            if (!TextUtils.isEmpty(friendInfo.userface)) {
                ImageLoader.getInstance().displayImage(friendInfo.userface, holder.icon);
            } else {
                holder.icon.setBackgroundResource(R.drawable.picture_loading);
            }

            holder.friendName.setText(friendInfo.username);
            holder.friendCount.setText(String.format("邀请人数%s人", friendInfo.invitenum));

            holder.date.setText(CommonUtil.formatTimeForZuixin(friendInfo.addtime));

            return convertView;
        }

        @Override
        public Object getItem(int position) {
            return myFriends.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        class ViewHolder {
            CircleImageView icon;
            TextView friendName;
            TextView friendCount;
            TextView date;
        }

        @Override
        public void nextPage(
                int start,
                int size,
                final com.xindaoapp.happypet.adapter.XinDaoBaseAdapter.ILoadNextPageData<FriendInfo> iLoadNextPageData) {
            getMoccaApi().getRequestFriends(new IRequest<MyRequestFriend>() {

                @Override
                public void request(MyRequestFriend t) {
                    iLoadNextPageData.loadNextPageData(t.array);
                }
            });
        }

    }

    @Override
    protected int getTopBarLeftImageViewRes() {
        return R.drawable.top_bar_back;
    }

    @Override
    protected OnClickListener getLeftViewClickListener() {
        return new OnClickListener() {

            @Override
            public void onClick(View v) {
                RequestFriendActivity.this.finish();
            }
        };
    }



}
